<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>首页</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
	<div class="layui-tab">
		<ul class="layui-tab-title">
			<li class="layui-this">磁力解析</li>
			<li>磁力搜索</li>
		</ul>
		<div class="layui-tab-content">
			<div class="layui-tab-item layui-show">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<label class="layui-form-label">磁力链接</label>
						<div class="layui-input-block">
							<input type="text" id="magnet" autocomplete="off"
								placeholder="请输入磁力链接,如：magnet:?xt=urn:btih:6066D593B4EB7DA2C9FA3040B7C17B799AB913C1"
								class="layui-input"
								value="magnet:?xt=urn:btih:6066D593B4EB7DA2C9FA3040B7C17B799AB913C1">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">种子解析</label>
						<div class="layui-upload-drag" id="uploadTorrent">
							<i class="layui-icon"></i>
							<p>点击上传，或将文件拖拽到此处</p>
						</div>
					</div>
					<div class="layui-form-item">
						<div class="layui-input-block">
							<button class="layui-btn" lay-submit="" id="magnetBtn">立即提交</button>
							<button type="reset" class="layui-btn layui-btn-primary">重置</button>
						</div>
					</div>
				</form>

				<table class="layui-hide" lay-filter="magnetTable" id="magnetTable"></table>
			</div>


			<div class="layui-tab-item">
				<h1 style="text-align: center;">还没做</h1>
			</div>
		</div>
	</div>
	<script type="text/html" id="barDemo">
  		<a class="layui-btn layui-btn-xs" lay-event="parseMagnet">解析</a>
		<a class="layui-btn layui-btn-xs" lay-event="play">播放</a>
	</script>
	<script src="layui/layui.js"></script>
	<script>
		layui.use([ 'form', 'upload', 'element', 'table' ], function() {
			var upload = layui.upload;//上传
			var element = layui.element; //元素操作
			var form = layui.form; //表单
			var $ = layui.jquery; //jq
			var table = layui.table; //table
			$("#magnetBtn").click(function() {
				layer.load(2);
				$.ajax({
					url : '/parseMagnet',
					type : "post",
					dataType : "json",
					data : {
						magnet : $("#magnet").val(),
					},
					async : false,
					success : function(data) {
						renderTable(data);
					}
				})
				layer.closeAll('loading');
				return false;
			});

			var renderTable = function(data) {
				table.render({
					elem : '#magnetTable',
					cols : [ [ {
						field : 'fileName',
						title : '文件名',
						minWidth : 120
					}, {
						field : 'fileSize',
						title : '文件大小',
						minWidth : 150
					}, {
						field : 'data',
						title : 'data',
						width : 80
					}, {
						field : 's1',
						title : 's1',
						width : 80
					}, {
						fixed : 'right',
						title : '操作',
						toolbar : '#barDemo',
						width : 150
					} ] ],
					data : data
				})
			}

			table.on('tool(magnetTable)', function(obj) {
				var data = obj.data;
				var layEvent = obj.event;
				if (layEvent === 'parseMagnet') {
					$.ajax({
						url : '/download',
						type : "post",
						dataType : "json",
						data : {
							data : data.data,
							s1 : data.s1
						},
						async : false,
						success : function(data) {
							if(data.downUrl!=null){
								var new_obj = $("<input value='"+data.downUrl+"'>");
							    $('body').before(new_obj);
								new_obj.select();
								document.execCommand("copy");
								new_obj.remove();
								layer.alert("下载地址：<br/>"+data.downUrl+"<br/><div style='text-align:center;color:red;'>已经成功复制到剪切板</div>")
							}else{
								layer.alert("解析失败，请重试或者换个资源");
							}
						}
					})
				} else if (layEvent === 'play') {
					layer.msg('我不会');
				}
			})

			upload.render({
				elem : '#uploadTorrent',
				url : '/uploadTorrent/',
				done : function(data) {
					renderTable(data);
				}
			    ,accept: 'file'
			});
		});
	</script>
</body>